﻿#region Namespaces

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    using NGenORM.Library.Enum;
    using NGenORM.Library.Code;
    using NGenORM.Library.Util;

#endregion


namespace NGenORM.Desktop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        #region variables
        private static bool Connected { get; set; }
        #endregion

        #region Objects
        private static Project Project = new Project();
        #endregion

        #region methods

        public void BindDataServer()
        {

            cmbDataserverType.Items.Clear();

            foreach (DBServerType a in Enum.GetValues(typeof(DBServerType)).Cast<DBServerType>().ToArray())
            {
                cmbDataserverType.Items.Add(a.ToString());
            }

        }

        public void LoadObjects()
        {
            DBRead DBRead = new DBRead();
            DBRead.Project = Project;
            lstObjects.ItemsSource = DBRead.ListAllObjects();
        }

        public void LoadGrid(int type)
        {
            // 1-Connection - 2-Projet - 3-Object - 4-Code
            switch (type)
            {
                case 1: // Connection
                    grdConnection.Visibility = System.Windows.Visibility.Visible;
                    grdProject.Visibility = System.Windows.Visibility.Hidden;
                    grdObjects.Visibility = System.Windows.Visibility.Hidden;
                    grdCode.Visibility = System.Windows.Visibility.Hidden;
                    grdHelp.Visibility = System.Windows.Visibility.Hidden;
                    grdAboutUs.Visibility = System.Windows.Visibility.Hidden;
                    break;
                case 2: // Project
                    grdConnection.Visibility = System.Windows.Visibility.Hidden;
                    grdProject.Visibility = System.Windows.Visibility.Visible;
                    grdObjects.Visibility = System.Windows.Visibility.Hidden;
                    grdCode.Visibility = System.Windows.Visibility.Hidden;
                    grdHelp.Visibility = System.Windows.Visibility.Hidden;
                    grdAboutUs.Visibility = System.Windows.Visibility.Hidden;
                    break;
                case 3: // Object
                    grdConnection.Visibility = System.Windows.Visibility.Hidden;
                    grdProject.Visibility = System.Windows.Visibility.Hidden;
                    grdObjects.Visibility = System.Windows.Visibility.Visible;
                    grdCode.Visibility = System.Windows.Visibility.Hidden;
                    grdHelp.Visibility = System.Windows.Visibility.Hidden;
                    grdAboutUs.Visibility = System.Windows.Visibility.Hidden;
                    break;
                case 4: // Code
                    grdConnection.Visibility = System.Windows.Visibility.Hidden;
                    grdProject.Visibility = System.Windows.Visibility.Hidden;
                    grdObjects.Visibility = System.Windows.Visibility.Hidden;
                    grdCode.Visibility = System.Windows.Visibility.Visible;
                    grdHelp.Visibility = System.Windows.Visibility.Hidden;
                    grdAboutUs.Visibility = System.Windows.Visibility.Hidden;
                    break;
                case 5: // AboutUs
                    grdConnection.Visibility = System.Windows.Visibility.Hidden;
                    grdProject.Visibility = System.Windows.Visibility.Hidden;
                    grdObjects.Visibility = System.Windows.Visibility.Hidden;
                    grdCode.Visibility = System.Windows.Visibility.Hidden;
                    grdAboutUs.Visibility = System.Windows.Visibility.Visible;
                    grdHelp.Visibility = System.Windows.Visibility.Hidden;
                    break;
                case 6: // Help
                    grdConnection.Visibility = System.Windows.Visibility.Hidden;
                    grdProject.Visibility = System.Windows.Visibility.Hidden;
                    grdObjects.Visibility = System.Windows.Visibility.Hidden;
                    grdCode.Visibility = System.Windows.Visibility.Hidden;
                    grdAboutUs.Visibility = System.Windows.Visibility.Hidden;
                    grdHelp.Visibility = System.Windows.Visibility.Visible;
                    break;
            }
        }

        public void BlockButtons(int type)
        {
            // 1-Connection - 2-Projet - 3-Object - 4-Code
            switch (type)
            {
                case 1: // Connection
                    btnMenuConnection.IsEnabled = true;
                    btnMenuProject.IsEnabled = false;
                    btnMenuObjects.IsEnabled = false;
                    btnMenuCode.IsEnabled = false;
                    break;
                case 2: // Project
                    btnMenuConnection.IsEnabled = true;
                    btnMenuProject.IsEnabled = true;
                    btnMenuObjects.IsEnabled = false;
                    btnMenuCode.IsEnabled = false;
                    break;
                case 3: // Object
                    btnMenuConnection.IsEnabled = true;
                    btnMenuProject.IsEnabled = true;
                    btnMenuObjects.IsEnabled = true;
                    btnMenuCode.IsEnabled = false;
                    break;
                case 4: // Code
                    btnMenuConnection.IsEnabled = true;
                    btnMenuProject.IsEnabled = true;
                    btnMenuObjects.IsEnabled = true;
                    btnMenuCode.IsEnabled = true;
                    break;
            }
        }

        public void LoadDatabases(Project Project) {
            DBRead DBRead = new DBRead();
            DBRead.Project = Project;

            cmbDatabase.Items.Clear();
            foreach (string db in DBRead.ListAllDatabase()) {
                cmbDatabase.Items.Add(db.ToString());
            }

        }

        #endregion

        #region events

        public MainWindow() {
            InitializeComponent();
        }

        private void menuExit_Click(object sender, RoutedEventArgs e) {
            MessageBoxResult mbr = MessageBox.Show("Deseja realmente sair?", "Sair do aplicativo", MessageBoxButton.YesNo);
            if (mbr == MessageBoxResult.Yes) {
                Close();
            }
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            BindDataServer();
            BlockButtons(1);
        }

        private void btnMenuConnection_Click(object sender, RoutedEventArgs e)
        {
            LoadGrid(1);
        }

        private void btnMenuProject_Click(object sender, RoutedEventArgs e)
        {
            LoadGrid(2);
        }

        private void btnMenuObjects_Click(object sender, RoutedEventArgs e)
        {
            LoadGrid(3);
        }

        private void btnMenuCode_Click(object sender, RoutedEventArgs e)
        {
            LoadGrid(4);
        }

        private void btnMenuAboutUs_Click(object sender, RoutedEventArgs e)
        {
            LoadGrid(5);
        }

        private void btnMenuHelp_Click(object sender, RoutedEventArgs e)
        {
            LoadGrid(6);
        }

        private void chkIntegrado_Checked(object sender, RoutedEventArgs e) {
            txtUserName.IsEnabled = false; 
            psbPassword.IsEnabled = false;
        }

        private void chkIntegrado_Unchecked(object sender, RoutedEventArgs e) {
            txtUserName.IsEnabled = true;
            psbPassword.IsEnabled = true;

            //txtClasse.Clear();

            //lsbProcedure.Items.Clear();
            //lsbTabela.Items.Clear();
            //lsbView.Items.Clear();

            cmbDatabase.Items.Clear();
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e) {

            try {

                Project.DBServerType = (DBServerType)Enum.Parse(typeof(DBServerType), cmbDataserverType.SelectedValue.ToString(), true);
                Project.CreateConnection();

                Project.ConnectionFactory.Datasource = txtServerName.Text.ToString();
                Project.ConnectionFactory.IntegratedSecurity = chkIntegratedSecurity.IsChecked.Value;

                if (Project.ConnectionFactory.IntegratedSecurity)
                {
                    txtUserName.Text = "";
                    psbPassword.Password = "";
                    lblPassCheck.Visibility = System.Windows.Visibility.Hidden;
                    lblUserCheck.Visibility = System.Windows.Visibility.Hidden;
                }
                else
                {
                    Project.ConnectionFactory.UserID = txtUserName.Text.ToString();
                    Project.ConnectionFactory.Password = psbPassword.Password.ToString();

                    if (txtUserName.Text.Length == 0 && psbPassword.Password.Length == 0)
                    {
                        lblPassCheck.Visibility = System.Windows.Visibility.Visible;
                        lblUserCheck.Visibility = System.Windows.Visibility.Visible;
                    }

                    if (txtUserName.Text.Length == 0)
                    {
                        lblUserCheck.Visibility = System.Windows.Visibility.Visible;
                        return;
                    }
                    else
                    {
                        lblUserCheck.Visibility = System.Windows.Visibility.Hidden;
                    }

                    if (psbPassword.Password.Length == 0)
                    {
                        lblPassCheck.Visibility = System.Windows.Visibility.Visible;
                        return;
                    }
                    else
                    {
                        lblPassCheck.Visibility = System.Windows.Visibility.Hidden;
                    }

                }

                if (Project.ConnectionFactory.TestConnection())
                {

                    Connected = true;

                    LoadDatabases(Project);

                    lblServerNameInfo.Content = String.Concat("Servidor: ", Project.ConnectionFactory.Datasource.ToString());

                    btnConnect.IsEnabled = false;
                    btnDisconnect.IsEnabled = true;
                    txtUserName.IsEnabled = false;
                    txtServerName.IsEnabled = false;
                    cmbDataserverType.IsEnabled = false;
                    cmbDatabase.IsEnabled = true;
                    psbPassword.IsEnabled = false;
                    chkIntegratedSecurity.IsEnabled = false;

                    BlockButtons(2);

                }
                else
                {
                    Connected = false;
                }

            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message.ToString(), "Erro ao efetuar a conexão", MessageBoxButton.OK);
            }

        }

        private void btnDisconnect_Click(object sender, RoutedEventArgs e) {

            Connected = false;

            BlockButtons(1);

            lblServerNameInfo.Content = "Servidor: não conectado.";
            lblDatabaseInfo.Content = "Base de dados: não selecionada.";

            cmbDatabase.IsEnabled = false;

            btnConnect.IsEnabled = true;
            btnDisconnect.IsEnabled = false;

            cmbDataserverType.IsEnabled = true;
            chkIntegratedSecurity.IsEnabled = true;

            if (chkIntegratedSecurity.IsChecked == false)
            {
                txtUserName.IsEnabled = true;
                psbPassword.IsEnabled = true;
            }

        }

        #endregion

        private void btnSaveProject_Click(object sender, RoutedEventArgs e) {
            BlockButtons(3);
            LoadObjects();
        }

        private void chkSelectHeader_Click(object sender, RoutedEventArgs e) {
            //foreach (GridViewRowPresenter gr in (GridViewRowPresenter)sender) { 
                
            //}
        }

        private void cmbDatabase_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            Project.ConnectionFactory.Database = cmbDatabase.SelectedValue.ToString();
            lblDatabaseInfo.Content = String.Concat("Base de dados: ", cmbDatabase.SelectedValue.ToString());
        }

    }
}

